package com.hymjweb.demo.hymj.basic_data_manage.xt_airport.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.bean.XtAirportPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.service.XtAirportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/basic_data_manage/xt_airport")
public class XtAirportController  extends BaseController {

    @Autowired
    private XtAirportService xtAirportService;

    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/basic_data_manage/xt_airport/xtairport";
    }

    /**
     * 根据输入的始发机场名称查询机场
     * @param key
     * @return
     */
    @RequestMapping("/queryAirPortByKey")
    @ResponseBody
    public List<XtAirportPojo> queryAirPortByKey(String key){
        List<XtAirportPojo> xtAirportPojos = xtAirportService.queryAirPortByKey(key);
        return xtAirportPojos;
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) throws Exception {
        if(param == null || param.isEmpty()){
            throwException("始发机场查询参数错误");
        }
        return xtAirportService.getPageList(param);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody(required = true)XtAirportPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("始发机场保存参数错误");
        }
        int affectRows = xtAirportService.insert(pojo);
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("始发机场保存成功!");
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("始发机场保存失败!");
        }
        return baseResultMap;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody(required = true)XtAirportPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("始发机场更新参数错误");
        }

        int flag = xtAirportService.update(pojo);

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("始发机场更新成功!");

        if (flag <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("始发机场更新失败!");
        }
        return baseResultMap;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestParam(required = true) Map map) throws Exception {
        if(map==null || map.isEmpty()){
            throwException("始发机场删除参数错误!");
        }

        String id = DataTypeUtil.toNotNullString(map.get("id"));
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("始发机场删除参数错误");
        }

        int affectRows = xtAirportService.delete(id);
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("始发机场删除成功!");

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("始发机场删除失败!");
        }
        return baseResultMap;
    }

}
